#include<bits/stdc++.h>
using namespace std;
const int n = 10,m=10;
int arr[n][m] ;

void init_arr(){
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            arr[i][j]=rand()%20 +1;
}

void print(){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++)
            printf("%2d ",arr[i][j]);
        puts("");
    }      
}

int dpArr[n][m];
int mov_x[2]={0,1},mov_y[2]={1,0};
void dp(){
    dpArr[0][0]=arr[0][0];
    for (int i = 0; i < n; i++)
    {      
        for (int j = 0; j < m; j++)
        { 
            if(i==0) dpArr[i][j] = dpArr[i][j-1]+ arr[i][j];
            else if(j==0)  dpArr[i][j] = dpArr[i-1][j]+ arr[i][j];
            else dpArr[i][j] = min(dpArr[i][j-1],dpArr[i-1][j]) + arr[i][j];
        }
    }
    printf("%d\n",dpArr[n-1][m-1]);
}

#include<stack>
stack<char*> q;
void route(){
    int x=n-1,y=m-1;
    while (x!=0 || y!=0)
    {
        // printf("(%d,%d) ",x,y);
        char xy[10] ={'\0'};
        sprintf(xy,"(%d,%d) ",x,y);
        q.push(xy);
        if(x==0 || dpArr[x][y] == dpArr[x][y-1]+ arr[x][y]) y--;
        else x--;
    }
    while (!q.empty())
    {
    //    printf("%s ",q.top());
       cout<<q.top();
       q.pop();
    }
}

int main()
{
    init_arr();
    print();
    dp();
    route();
    return 0;
}
